﻿// Inner Fire 游戏引擎库
// Lua绑定适配项目
//
// Copyright (c) 2025 by 尔西大帝. All rights reserved.
//
// 所属层级：应用层(4)
// 用于图形系统的Lua绑定适配。
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2025-06-17

#include "lua_manager.h"
#include <game/manager/game_manager.h>

namespace ifire::game {

static int Graphics_GetEyePos(lua_State* L) {
  auto ptr = ikit::lua_get_self<dx::DxGraphics>(L, "GraphicsMeta");
  return ikit::lua_return_float3(L, ptr->GetView().GetEyePos());
}

static int Graphics_SetCameraDistance(lua_State* L) {
  auto ptr = ikit::lua_get_self<dx::DxGraphics>(L, "GraphicsMeta");
  ptr->GetView().GetSpherical().SetRadius(L_FLOAT(2));
  return 0;
}

static int Graphics_SetCameraCoord(lua_State* L) {
  auto ptr = ikit::lua_get_self<dx::DxGraphics>(L, "GraphicsMeta");
  ptr->GetView().GetSpherical().SetCoord(L_FLOAT(2), L_FLOAT(3), L_FLOAT(4));
  return 0;
}

static int Graphics_GetCameraCoord(lua_State* L) {
  auto ptr = ikit::lua_get_self<dx::DxGraphics>(L, "GraphicsMeta");
  return ikit::lua_return_float3(L, ptr->GetView().GetSpherical().GetCoord());
}

static int Graphics_SetCameraOffset(lua_State* L) {
  auto ptr = ikit::lua_get_self<dx::DxGraphics>(L, "GraphicsMeta");
  ptr->GetView().GetSpherical().SetOffset(IVec3(L_FLOAT(2), L_FLOAT(3), L_FLOAT(4)));
  return 0;
}

static int Graphics_GetCameraOffset(lua_State* L) {
  auto ptr = ikit::lua_get_self<dx::DxGraphics>(L, "GraphicsMeta");
  return ikit::lua_return_float3(L, ptr->GetView().GetSpherical().GetOffset());
}

void register_lua_graphics(lua_State* L, LuaManager* lua_manager) {
  // 注册DxGraphics
  luaL_Reg scene_methods[] = {           //
      {"GetEyePos", Graphics_GetEyePos}, //
      {"SetCameraDistance", Graphics_SetCameraDistance}, //
      {"SetCameraCoord", Graphics_SetCameraCoord}, //
      {"GetCameraCoord", Graphics_GetCameraCoord}, //
      {"SetCameraOffset", Graphics_SetCameraOffset}, //
      {"GetCameraOffset", Graphics_GetCameraOffset}, //

      {NULL, NULL}};
  ikit::lua_register_metatable(L, "GraphicsMeta", scene_methods);
  ikit::lua_set_global_object(
      L, lua_manager->GM()->GetGraphics(), "Graphics", "GraphicsMeta");
}

} // namespace ifire::game